﻿using System;
using System.Web.UI.WebControls;
using EntitiesTM;
using Business;
using System.Data;

namespace TimeWork
{
    public partial class MyProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["email"] != null)
            {
                if (!IsPostBack)
                {
                    pnlNotice.Visible = false;
                    LoadContent();
                }
            }
            else
            {
                Response.Redirect("http://"+Request.Url.Authority+"/Login.aspx");
            }
        }
        public void LoadContent()
        {
            try
            {
                string email = Session["email"].ToString();
                lblEmail.Text = email;
                EnStaff staff = BuStaff.selStaffByEmail(email, Session["companyid"].ToString());
                txtName.Text = staff.staffName;
                txtAddress.Text = staff.staffAddress;
                if (staff.staffBirthday.ToString("dd/MM/yyyy").Equals("01/01/1754"))
                {
                    txtBirthday.Text = "";
                }
                else
                {
                    txtBirthday.Text = staff.staffBirthday.ToString("dd/MM/yyyy");
                }
                txtLevel.Text = staff.staffLevel.ToString();
                txtPhone.Text = staff.staffPhone;
                txtScale.Text = staff.staffScale;
                txtPosition.Text = staff.staffPosition;

                ddlGender.DataBind();
                ddlGender.SelectedValue = staff.staffGender.ToString();

                DataTable dataResultUnit = BuResultUnit.selAllResultUnit(Session["companyid"].ToString());
                if (dataResultUnit.Rows.Count != 0)
                {
                    ddlResultUnit.DataSource = dataResultUnit;
                    ddlResultUnit.DataTextField = "ResultUnitName";
                    ddlResultUnit.DataValueField = "ResultUnitID";
                    ddlResultUnit.DataBind();
                    ddlResultUnit.SelectedValue = staff.staffResultUnitID.ToString();
                }
                else
                {
                    ddlResultUnit.DataBind();
                    ddlResultUnit.Items.Insert(0, new ListItem("--- Chọn đơn vị ---", "0"));
                    ddlResultUnit.SelectedValue = "0";
                }

            }
            catch
            {
                pnlChangeProfile.Visible = false;
                lblError.Text = "Không tồn tại Email này!";
            }

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            pnlNotice.Visible = true;
            string name = txtName.Text.Trim();
            txtName.CssClass = "error";
            if (!name.Equals(""))
            {
                try
                {
                    EnStaff staff = new EnStaff();
                    txtName.CssClass = "valid";
                    staff.staffEmail = lblEmail.Text;
                    staff.staffName = name;
                    staff.staffResultUnitID = Int32.Parse(ddlResultUnit.SelectedValue);
                    try
                    {
                        staff.staffBirthday = DateTime.ParseExact(txtBirthday.Text, "dd/MM/yyyy", null);
                    }
                    catch
                    {
                        staff.staffBirthday = DateTime.Parse("1/1/1754");
                    }
                    staff.staffGender = Boolean.Parse(ddlGender.SelectedValue);
                    staff.staffPhone = txtPhone.Text.Trim();
                    staff.staffAddress = txtAddress.Text.Trim();
                    staff.staffPosition = txtPosition.Text.Trim();
                    staff.staffScale = txtScale.Text.Trim();
                    staff.staffLevel = Int32.Parse(txtLevel.Text);               
                    staff.staffCompanyID = Int32.Parse(Session["companyid"].ToString());

                    BuStaff.updMyProfile(staff, Session["companyid"].ToString());
                    LoadContent();
                    imgNotice.ImageUrl = "~/resources/img/complete.png";
                    lblNotice.Text = "Cập nhật thành công";
                    lblNotice.CssClass = "success";

                }
                catch
                {
                    imgNotice.ImageUrl = "~/resources/img/fail.png";
                    lblNotice.Text = "Cập nhật thất bại";
                    lblNotice.CssClass = "fail";
                }
            }
            else
            {
                imgNotice.ImageUrl = "~/resources/img/fail.png";
                lblNotice.Text = "Cập nhật thất bại";
                lblNotice.CssClass = "fail";
            }
        }

        protected void btnChangePass_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChangePwd.aspx");
        }
    }
}